/*
 * @Author       : DeaneChen
 * @Date         : 2021-08-19 11:07:56
 * @LastEditTime : 2021-08-19 13:30:44
 * @LastEditors  : DeaneChen
 * @Description  : 
 * @FilePath     : \master_controller\User\delay.c
 * HomePage     : https://luheqiu.com
 */
#include "delay.h"

static __IO uint32_t TimingDelay;  //SysTick计数变量

#if defined MPU6050     //如果使用MPU6050
__IO uint32_t timeStamp_ms=0;      //毫秒绝对时间
uint8_t timeStamp_ms_counter=0;
#endif

/*全局变量TimingDelay减一函数，直到0为止*/
void TimingDelay_Decrement(void)
{
    if (TimingDelay != 0x00)
    { 
    TimingDelay--;
    }
  
#if defined MPU6050     //如果使用MPU6050
    timeStamp_ms++;
    if(timeStamp_ms_counter >= 10)
    {
        timeStamp_ms++;
        timeStamp_ms_counter = 0;
    }
#endif
	
}

/*延时nTime*100微秒*/
void Delay_100us(__IO uint32_t nTime)
{ 
    TimingDelay = nTime;
    while(TimingDelay != 0);   //一直等到TimingDelay减到0为止
}

/*延时nTime毫秒*/
void Delay_ms(__IO uint32_t nTime)
{ 
	uint32_t i;
	i=nTime;
	while(i--)            //一直等到i减到0为止，i每Delay_10us(100)即1毫秒减一
		Delay_100us(10);
}

#if defined MPU6050     //如果使用MPU6050
int stm32_get_clock_ms(unsigned long *count)
{
    if (!count)
        return 1;
    count[0] = timeStamp_ms;
    return 0;
}
#endif
